package com.cqhilink.iot.jobstandardmanagement.services.innerservice.impl.file;

import com.alibaba.fastjson.JSON;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.iot.jobstandardmanagement.common.enums.FileTypeUtil;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.FileMapper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.File;
import com.cqhilink.iot.jobstandardmanagement.facade.service.file.JsmFileService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangcs on 2016/8/8.
 * 文件信息
 */

public class JsmFileServiceImpl implements JsmFileService {

    @Autowired
    private FileMapper fileMapper;

    /**
     * @param businessId  关联did
     * @param filenames 文件名称
     * @param hbasepaths 文件路径
     * @return
     */
    @Override
    public int insertFile(String businessId, String[] filenames, String[] hbasepaths) {
        return 0;
    }

    /**
     * @param oldBusinessId 原来的业务id
     * @param newBusinessId 新增的业务id
     * @return
     */
    @Override
    public int cloneInsert(String oldBusinessId, String newBusinessId) {
        int resInt = 0;
        List<File> files = fileMapper.queryByBusinessId(oldBusinessId);
        if (files != null && files.size() > 0) {
            for (File file : files) {
                file.setFileId(UniqueUtil.uuid());
                file.setBusinessId(newBusinessId);
                resInt = resInt + fileMapper.insertSelective(file);
            }
        }
        return resInt;
    }

    /**
     * @param businessId 关联id
     * @return
     */
    @Override
    public int delFileByBusinessId(String businessId) {
        return 0;
    }
    /**
     * @param businessId 关联id
     * @return
     */
    @Override
    public List<File> queryFilesByBusinessId(String businessId) {
        return null;
    }
    /**
     * @param fileId 关联id
     * @return
     */
    @Override
    public File selectByPrimaryKey(String fileId) {
        return fileMapper.selectByPrimaryKey(fileId);
    }

    /**
     * 对文件类型做处理
     *
     * @param fileType
     * @return
     */
    public int convertFileType(String fileType) {
        if (fileType == null || "".equals(fileType)) return -1;
        if (fileType.equals("doc")) return 1;
        if (fileType.equals("rar")) return 2;
        if (fileType.equals("pdf")) return 3;
        if (fileType.equals("zip")) return 4;
        if (fileType.equals("xls")) return 5;
        if (fileType.equals("docx")) return 6;
        if (fileType.equals("xlsx")) return 7;
        //视频格式
        if (fileType.equals("rm")) return 8;
        if (fileType.equals("rmvb")) return 9;
        if (fileType.equals("wmv")) return 10;
        if (fileType.equals("avi")) return 11;
        if (fileType.equals("mp4")) return 12;
        if (fileType.equals("3gp")) return 13;
        if (fileType.equals("mkv")) return 14;

        return -1;
    }

    /**
     * 根据业务id查询附件表
     *
     * @param businessId
     * @return
     */
    @Override
    public List<File> queryByBusinessId(String businessId) throws Exception {
        if (StringUtils.isEmpty(businessId)) {
            return null;
        }
        return fileMapper.queryByBusinessId(businessId);
    }
    /**
     * @param businessId  关联id
     * @param uploadFiles 上传文件
     * @param deletedFiles 删除文件
     * @return
     */
    @Override
    public int addFile(String uploadFiles, String deletedFiles, String businessId) throws Exception {
        int a = 0;
        List<File> newList = new ArrayList<>();
        if (uploadFiles != null && !uploadFiles.equals("")) {
            //强制转换成集合
            List<File> listFiles = JSON.parseArray(uploadFiles, File.class);
            if (listFiles != null && listFiles.size() > 0) {
                //重新设置附件属性
                int len = listFiles.size();
                // 文件 内容名称
                for (int i = 0; i < len; i++) {
                    File _file = listFiles.get(i);
                    //设置主键
                    _file.setFileId(UniqueUtil.uuid());
                    //设置业务数据id
                    _file.setBusinessId(businessId);
                    _file.setFileType((short) FileTypeUtil.getCode(_file.getExt()));
                    newList.add(_file);
                }
                //  必须判断非空
                if (newList != null && newList.size() > 0) {
                    a = fileMapper.insertByList(newList);
                }
            }
        }

        return a;
    }

    /**
     * 新增附件，并且删除其他无用附件信息
     *
     * @param uploadFiles
     * @param businessId
     * @return int
     * @throws Exception
     * @Author 卢曼成
     */
    @Override
    public int addDelFile(String uploadFiles, String businessId) throws Exception {
        int a = 0;
        List<File> newList = new ArrayList<>();
        if (uploadFiles != null && !uploadFiles.equals("")) {
            List<File> listFiles = JSON.parseArray(uploadFiles, File.class);
            if (listFiles != null && listFiles.size() > 0) {
                fileMapper.delByBusinessId(businessId);//删除原有头像数据
                //重新设置附件属性
                int len = listFiles.size();
                for (int i = 0; i < len; i++) {
                    File _file = listFiles.get(i);
                    //设置主键
                    _file.setFileId(UniqueUtil.uuid());
                    //设置业务数据id
                    _file.setBusinessId(businessId);
                    _file.setFileType((short) FileTypeUtil.getCode(_file.getExt()));
                    newList.add(_file);
                }
                //  必须判断非空
                if (newList != null && newList.size() > 0) {
                    a = fileMapper.insertByList(newList);
                }
            }
        }
        return a;
    }

    @Override
    public int updateFile(String uploadFiles, String deletedFiles, String businessId) throws Exception {
        //更新附件信息
        int a = addFile(uploadFiles, deletedFiles, businessId);
        //需要删除的附件信息
        if (deletedFiles != null && !deletedFiles.equals("")) {
            //强制转换成集合
            List<File> listFiles = JSON.parseArray(deletedFiles, File.class);
            if (listFiles != null && listFiles.size() > 0) {
                int len = listFiles.size();
                for (int i = 0; i < len; i++) {
                    File _file = listFiles.get(i);
                    fileMapper.deleteByPrimaryKey(_file.getFileId());
                }
            }
        }
        return a;
    }

    /**
     * 批量删除附件信息
     *
     * @param businessId
     * @return
     */
    @Override
    public int delByBusinessId(String businessId) {
        return fileMapper.delByBusinessId(businessId);
    }

    /**
     * 根据businessId集合删除
     *
     * @param list
     * @return
     */
    @Override
    public int deleteListString(List<String> list) {
        return fileMapper.deleteListString(list);
    }

    /**
     * 批量插入
     *
     * @param list
     * @return
     */
    @Override
    public int insertList(List<File> list) {
        return fileMapper.insertList(list);
    }

}
